package com.boss.core;

import com.boss.core.Boss;
import hudson.Extension;
import hudson.model.RootAction;
import hudson.model.User;

import java.util.List;

/**
 * Entry point to BOSS.
 * This provides a link for Boss in the side menu of Hudson homepage.
 * On the Boss homepage it populates the view dynamically by finding the associations
 * for Boss class.
 *
 * @author Momin Noor Khan
 */

@Extension
public class BossHome implements RootAction {

    /**
     * Gets the relative path of the image used as an icon for this class on Hudson's side menu.
     */
    public String getIconFileName() {
        if (User.current() != null)  //temporary workaround to hide the side menu icon for boss
        {
            return "/plugin/boss/images/boss.gif";
        } else {
            return null;
        }
    }

    /**
     * Gets the display name of this view (for Hudson's side menu primarily)
     */
    public String getDisplayName() {
        return "BOSS";
    }

    /**
     * Gets the relative URL of the Boss HomePage
     */
    public String getUrlName() {
        if (User.current() != null) {
            return "/boss";
        } else {
            return "login?from=%2F";
        }
    }

    /**
     * Finds out if the logged in user is authorized to perform GIS tasks.
     */
    public static boolean isAuthorizedGISUser() {
        return Boss.isAuthorizedGISUser();
    }

    /**
     * Gets the class associated with Boss which has the same url as the passed parameter.
     *
     * @param name
     * @return
     */
    public Boss getDynamic(String name) {
        for (Boss b : getAll()) {
            if (b.getUrlName().equals(name)) {
                return b;
            }
        }
        return null;
    }

    /**
     * Gets the list of all classes associated with type 'Boss'
     *
     * @return
     */
    public List<Boss> getAll() {
        return Boss.all();
    }
}


